package com.example.widget_lib.widget.progress.horbar;

import com.example.widget_lib.utils.LogUtil;
import com.example.widget_lib.utils.PxUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;

/**
 * Created by Allen on 2018/1/31.
 * <p>
 * 线性加载动画
 */

public class LoadingLineView extends Component implements Component.DrawTask{

    private Context mContext;
    private int mWidth;
    private int mHeight;

    /**
     * 动画起点x坐标
     */
    private int centerX;
    /**
     * 动画起点y坐标
     */
    private int centerY;

    /**
     * 偏移距离
     */
    private float dis;

    /**
     * view真实高度
     */
    private int mViewHeight;

    /**
     * 背景色画笔
     */
    private Paint bgPaint;
    /**
     * loading画笔
     */
    private Paint loadingPaint;

    /**
     * 画笔宽度（等于vie高度）
     */
    private int paintWidth;

    /**
     * 底色
     */
    private int bgColor = 0xFFe1e5e8;

    /**
     * loading颜色
     */
    private int loadingColor = 0xFF58B5FA;


    /**
     * 动画
     */
    private AnimatorValue loadingAnimator;
    /**
     * 动画执行时间
     */
    private int duration = 800;
    /**
     * 动画延时启动时间
     */
    private int startDelay = 0;

    /**
     * 是否停止动画（恢复初始状态）
     */
    private boolean isStopAnimation = false;

    public LoadingLineView(Context context) {
        this(context, null);
    }

    public LoadingLineView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        mContext = context;
        getAtt(attrSet);
//        init();
        initLoadingAnimation();
        addDrawTask(this);
    }

   /* public LoadingLineView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        getAtt(attrs);
//        init();
        addDrawTask(this);
    }*/

    /**
     * 想实现颜色等参数可配置，在这里实现配置即可,笔者就不多写了
     *
     * @param attrs attrs
     */
    private void getAtt(AttrSet attrs) {

    }

    private void init() {
        paintWidth = (int) PxUtil.vp2px(2);
//        mViewHeight = paintWidth;
        bgPaint = getPaint(paintWidth, bgColor, Paint.Style.FILL_STYLE);
        loadingPaint = getPaint(paintWidth, loadingColor, Paint.Style.FILL_STYLE);
        onSizeChanged();
    }

    /**
     * 统一处理paint
     *
     * @param strokeWidth 画笔宽度
     * @param color       颜色
     * @param style       风格
     * @return paint
     */
    private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
//        Paint.ANTI_ALIAS_FLAG
        Paint paint = new Paint();
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(new Color(color));
        paint.setAntiAlias(true);
        paint.setStyle(style);
        return paint;
    }

    public void onSizeChanged() {

        mWidth = getWidth();
        mHeight =getHeight();

        centerX =  getWidth() / 2;
        centerY =  getHeight() / 2;

    }



    /**
     * 初始化动画
     */
    private void initLoadingAnimation() {
        final float loadingMoveDistance = mWidth / 2;
//        loadingAnimator = ValueAnimator.ofFloat(0, loadingMoveDistance);
        loadingAnimator = new AnimatorValue();
        loadingAnimator.setDuration(duration);
        loadingAnimator.setDelay(startDelay);
//        loadingAnimator.setRepeatCount(ValueAnimator.INFINITE);
//        loadingAnimator.setInterpolator(new LinearInterpolator());
        loadingAnimator.setLoopedCount(AnimatorValue.INFINITE);
        loadingAnimator.setCurveType(AnimatorValue.CurveType.LINEAR);

        loadingAnimator.setValueUpdateListener((animatorValue, v) -> {
            dis = (getWidth() / 4) * v;
            if (dis <= loadingMoveDistance / 2) {
                loadingPaint.setAlpha((int) ((255 * dis) * 2 / loadingMoveDistance));
            } else {
                loadingPaint.setAlpha((int) (255 - (255 * dis) * 2 / loadingMoveDistance));
            }
            invalidate();
        });
    }

    /**
     * 开启动画
     */
    public void startLoading() {
        if (loadingAnimator != null) {
            loadingAnimator.start();
        }
        isStopAnimation = false;
    }

    /**
     * 结束动画
     */
    public void stopLoading() {
        if (loadingAnimator != null) {
            loadingAnimator.cancel();
        }
        isStopAnimation = true;
        invalidate();
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this,mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        init();

        /**
         * 画底色
         */
        Point pointStart = new Point(0,centerY);
        Point endStart = new Point(mWidth,centerY);
        canvas.drawLine(pointStart, endStart, bgPaint);

        if (!isStopAnimation) {
            /**
             * loading向左扩散
             */
            Point leftStart = new Point(centerX,centerY);
            Point leftEnd = new Point(centerX - dis,centerY);
            canvas.drawLine(leftStart, leftEnd, loadingPaint);
            /**
             * loading向右扩散
             */
            Point rightStart = new Point(centerX,centerY);
            Point rightEnd = new Point(centerX + dis,centerY);
            canvas.drawLine(rightStart, rightEnd, loadingPaint);
        }
    }
}
